﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using JuCheap.Core.Data;
using JuCheap.Core.Data.Entity;
using JuCheap.Core.Infrastructure;
using JuCheap.Core.Interfaces;
using JuCheap.Core.Models;
using JuCheap.Core.Models.Enum;
using JuCheap.Core.Models.Filters;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.Infrastructure.Utilities;
using Microsoft.EntityFrameworkCore;
using System;

namespace JuCheap.Core.Services.AppServices
{
    public class ShowPushService : IShowPushService
    {
        private readonly JuCheapContext _context;
        private readonly IMapper _mapper;

        public ShowPushService(JuCheapContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(ShowPushDto dto)
        {
            var entity = _mapper.Map<ShowPushDto, ShowPushEntity>(dto);

            entity.Init();

            var dbSet = _context.ShowPush;

            dbSet.Add(entity);

            return await _context.SaveChangesAsync() > 0 ? entity.Id : -1;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(ShowPushDto dto)
        {
            var dbSet = _context.ShowPush;

            var entity = await dbSet.FirstOrDefaultAsync(s => s.Id == dto.Id);

            entity.Contents = dto.Contents;

            entity.Texts = dto.Texts;

            entity.Push = dto.Push;

            return await _context.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(IEnumerable<string> ids)
        {
            var dbSet = _context.ShowPush;

            var entity = dbSet.Where(s => ids.Contains(s.Id.ToString()));

            entity.ForEach(e => e.IsDeleted = true);

            return await _context.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 分页获取列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public async Task<PagedResult<ShowPushDto>> SearchAsync(ShouPushFilter filter)
        {
            if (null == filter)
            {
                return new PagedResult<ShowPushDto>(0, 0);
            }

            var query = _context.ShowPush.AsQueryable();

            return await query.OrderByDescending(x => x.CreateDateTime).Where(x => x.IsDeleted == false)
                .Select(item => new ShowPushDto
                {
                    Id = Convert.ToInt32(item.Id),
                    Contents = item.Contents,
                    Texts = item.Texts,
                    CreateDateTime = item.CreateDateTime,
                    Time = item.CreateDateTime.ToString("yyyy-MM-dd"),
                    Push = item.Push,
                    State = item.Push == 0 ? "否" : "是"
                }).PagingAsync(filter.page, filter.rows);

        }
        /// <summary>
        /// 通过id获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ShowPushDto> FindAsync(int id)
        {
            var dbSet = _context.ShowPush;
            var entity = await dbSet.FirstOrDefaultAsync(r => r.Id == id);
            var dto = _mapper.Map<ShowPushEntity, ShowPushDto>(entity);
            return dto;
        }
        /// <summary>
        /// 获取发布为1的总条数
        /// </summary>
        /// <returns></returns>
        public async Task<int> GetPushCount()
        {
            var query = _context.ShowPush.AsQueryable();
            return await query.Where(x => x.Push == 1).CountAsync();
        }
    }
}
